from PyQt4 import QtGui, QtCore
import event_viewer_base
import lucid.core
from pydispatch import dispatcher

class EventViewerPlugin(lucid.core.IViewPlugin):
   """ Viewer to display all events flowing through the system.
       Intercepts pydispatch signals and could be extended in the future
       to intercept all qt events.
   """
   def __init__(self):
      lucid.core.IViewPlugin.__init__(self)
      self.widget = EventViewerWidget(self)
      
      # Connect to pydispatch to get all events.
      dispatcher.connect(self.recvEvent)
      
   @staticmethod
   def getName():
      return "Event Viewer"
      
   def getViewWidget(self):
      return self.widget
   
   def recvEvent(self, signal, sender, *arg, **kw):
      sender_name = str(sender)
      sig_name = str(signal)
      arg_str = str(arg)
      kw_str = str(kw)
      
      desc = "%s [%s]: %s %s\n"%(sender_name,sig_name,arg_str,kw_str)
      self.widget.mText.append(desc)
   
   def getIcon(self):
      return QtGui.QIcon(":/icons/data/icons/messages_16.png")
      
      
class EventViewerWidget(QtGui.QWidget, event_viewer_base.Ui_Form):
   def __init__(self, plugin, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.setupUi(self)
      self.plugin = plugin

      # Register signal to highlight node.
      self.connect(self.mClearButton, QtCore.SIGNAL("pressed()"), self.onClearPressed)

   def onClearPressed(self):
      self.mText.clear()
      
   